package com.lgs.demo.customview;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by lgs on 2018/3/11.
 */

public class NoScrollViewPager extends ViewPager {

    public NoScrollViewPager(Context context) {
        this(context, null);
    }

    public NoScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private int mLastX;
    private int mLastY;

    /**
     * getRowX()、getRowY()获取的是android坐标系,屏幕最左上角的点
     * getX()、getY()获取的是视图坐标系，父视图的左上角位置
     *
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        super.onInterceptTouchEvent(ev);
        final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //记录按下的位置
                mLastX = (int) ev.getRawX();
                //                mLastY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                //记录结束位置
                int moveX = (int) ev.getRawX();
                //                int moveY = (int) ev.getRawY();
                break;
        }
        if (mLastX < mLastX / 2) {
            //说明手指在屏幕中间靠左是要滑动ViewPager
            //事件由ViewPager自己消费
            return super.onInterceptTouchEvent(ev);
        } else {
            //说明手指在屏幕中间靠右是要向下传递事件
            return true;
        }
    }
}
